package com.jxb.dialog.utils;


import android.content.Context;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatTextView;

import com.jxb.dialog.BaseDialog;
import com.jxb.dialog.R;

/**
 * @author jinxb
 * date     : 2021/5/7
 * desc     :
 */
public class ConfirmDialogUtils {

    private ConfirmDialogUtils() {

    }

    public static ConfirmDialogUtils getInstance() {
        return Holder.sInstance;
    }

    private static class Holder {
        private static final ConfirmDialogUtils sInstance = new ConfirmDialogUtils();
    }

    private ConfirmDialog confirmDialog;

    public void showConfirmDialog(Context context, String content, BaseDialog.OnBtnClickListener onConfirmListener) {
        showConfirmDialog(context,content,onConfirmListener,null);
    }

    public void showConfirmDialog(Context context, String content, BaseDialog.OnBtnClickListener onConfirmListener,
                                  BaseDialog.OnBtnClickListener onCancelListener) {
        showConfirmDialog(context,"提示",content,onConfirmListener,onCancelListener);
    }

    public void showConfirmDialog(Context context, String title, String content,
                                  BaseDialog.OnBtnClickListener onConfirmListener) {
        showConfirmDialog(context,title,content,onConfirmListener,null);
    }

    public void showConfirmDialog(Context context, String title, String content,
                                  BaseDialog.OnBtnClickListener onConfirmListener,
                                  BaseDialog.OnBtnClickListener onCancelListener) {
        confirmDialog = new ConfirmDialog(context, title, content, onConfirmListener, onCancelListener);
        confirmDialog.show();
    }

    public void dismiss() {
        if (confirmDialog != null && confirmDialog.isShowing()) {
            confirmDialog.dismiss();
        }
    }


    private class ConfirmDialog extends BaseDialog {
        private String title = "";
        private String content = "";
        private OnBtnClickListener onConfirmListener;
        private OnBtnClickListener onCancelListener;
        private AppCompatTextView titleTextView;
        private AppCompatTextView cancelTextView;
        private AppCompatTextView confirmTextView;
        private AppCompatTextView contentTextView;

        public ConfirmDialog(@NonNull Context context, String title, String content,
                             OnBtnClickListener onConfirmListener, OnBtnClickListener onCancelListener) {
            super(context);
            this.title = title;
            this.content = content;
            this.onConfirmListener = onConfirmListener;
            this.onCancelListener = onCancelListener;
        }

        @Override
        protected int getContentView() {
            return R.layout.dialog_confirm;
        }

        @Override
        protected void initViews() {
            titleTextView = findViewById(R.id.titleTextView);
            contentTextView = findViewById(R.id.contentTextView);
            cancelTextView = findViewById(R.id.cancelTextView);
            confirmTextView = findViewById(R.id.confirmTextView);
        }

        @Override
        protected void initListeners() {
            cancelTextView.setOnClickListener(v -> {
                if (onCancelListener != null) {
                    onCancelListener.onClick();
                }
                dismiss();
            });
            confirmTextView.setOnClickListener(view1 -> {
                if (onConfirmListener != null) {
                    onConfirmListener.onClick();
                }
                dismiss();
            });
        }

        @Override
        public void show() {
            super.show();
            titleTextView.setText(title);
            contentTextView.setText(content);
        }
    }
}
